package com.bh.web.controller.wms;

import com.bh.common.annotation.Log;
import com.bh.common.core.controller.BaseController;
import com.bh.common.core.domain.AjaxResult;
import com.bh.common.core.page.TableDataInfo;
import com.bh.common.enums.BusinessType;
import com.bh.common.utils.poi.ExcelUtil;
import com.bh.wms.domain.WmsWarehouse;
import com.bh.wms.service.IWmsWarehouseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 仓库管理接口
 *
 * @author GPT
 */
@Api(tags = "仓库管理")
@RestController
@RequestMapping("/wms/warehouse")
public class WmsWarehouseController extends BaseController {

    @Autowired
    private IWmsWarehouseService warehouseService;

    @ApiOperation("分页查询仓库")
    @PreAuthorize("@ss.hasPermi('wms:warehouse:list')")
    @GetMapping("/list")
    public TableDataInfo list(WmsWarehouse query) {
        startPage();
        List<WmsWarehouse> list = warehouseService.selectWmsWarehouseList(query);
        return getDataTable(list);
    }

    @ApiOperation("导出仓库")
    @PreAuthorize("@ss.hasPermi('wms:warehouse:export')")
    @Log(title = "仓库管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmsWarehouse query) {
        List<WmsWarehouse> list = warehouseService.selectWmsWarehouseList(query);
        ExcelUtil<WmsWarehouse> util = new ExcelUtil<>(WmsWarehouse.class);
        util.exportExcel(response, list, "仓库数据");
    }

    @ApiOperation("获取仓库详情")
    @PreAuthorize("@ss.hasPermi('wms:warehouse:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        return success(warehouseService.selectWmsWarehouseById(id));
    }

    @ApiOperation("新增仓库")
    @PreAuthorize("@ss.hasPermi('wms:warehouse:add')")
    @Log(title = "仓库管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WmsWarehouse entity) {
        return toAjax(warehouseService.insertWmsWarehouse(entity));
    }

    @ApiOperation("修改仓库")
    @PreAuthorize("@ss.hasPermi('wms:warehouse:edit')")
    @Log(title = "仓库管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WmsWarehouse entity) {
        return toAjax(warehouseService.updateWmsWarehouse(entity));
    }

    @ApiOperation("修改仓库状态")
    @PreAuthorize("@ss.hasPermi('wms:warehouse:edit')")
    @Log(title = "仓库管理", businessType = BusinessType.UPDATE)
    @PutMapping("/status")
    public AjaxResult changeStatus(@RequestBody WmsWarehouse entity) {
        return toAjax(warehouseService.changeStatus(entity));
    }

    @ApiOperation("删除仓库")
    @PreAuthorize("@ss.hasPermi('wms:warehouse:remove')")
    @Log(title = "仓库管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(warehouseService.deleteWmsWarehouseByIds(ids));
    }
}

